angular.module('panels')
.directive("panelItilTodotaskDeptCount", function ($http, loadConf, $timeout, $log, moment, Promise, LoginInfo, tabService) {
    return {
        scope: {},
        link: function (scope) {
            function getHighChartOptions(store) {
                return {
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    type: 'pie'
                },
                colors: Highcharts.map(Highcharts.getOptions().colors, function (color) {
                    return {
                        radialGradient: {cx: 0.5, cy: 0.3, r: 0.7},
                        stops: [
                            [0, color],
                            [1, Highcharts.Color(color).brighten(-0.1).get('rgb')] // darken
                        ]
                    };
                }),
                title: {
                    text: ''
                },
                legend: {
                    y: -20
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                plotOptions: {
                    pie: {
                      allowPointSelect: true,
                      cursor: 'pointer',
                      dataLabels: {
                        enabled: true,
                        format: '{point.y}({point.percentage:.2f}%)'
                      },
                      showInLegend: true
                    }
               },
               series: [{
    	               type: 'pie',
    	               name: '待处理工单量占比',
    	               data: store
    	           }]
                };
            }

            scope.options = getHighChartOptions([]);
            scope.panelTitle = '部门待处理工单统计图';
            Promise.all({
                setting: loadConf('panel-itil-todotask-dept-count.yml'),
                loginInfo: LoginInfo
            }).then(function (result) {
                var setting = result.setting, loginInfo = result.loginInfo, preData, timeoutId;
                scope.more = setting.more;
                scope.$on('$destroy', function () {
                    timeoutId && $timeout.cancel(timeoutId);
                });
                (function updateData() {
                    $http.jsonp(setting.countQueryUrl).success(function (data) {
                    	$log.debug(data);
                    	if (angular.equals(data, preData)) {
                            return false;
                        } else {
                            preData = data;
                            data = angular.copy(data);
                        }

                        // 通知更新charts
                        scope.options = getHighChartOptions(data);
                    }).then(callback, callback);
                    function callback() {
                        if (setting.updateInterval > 0) {
                            timeoutId = $timeout(updateData, setting.updateInterval * 1000);
                        }
                    }

                    $log.debug('panel update data: itil-todotask-dept-count');
                }());
            });   
        },
        templateUrl: 'app/panel/highcharts-default.html'
    };
});